package com.scs.application.core.pdf.handler;

import com.itextpdf.io.font.constants.StandardFonts;
import com.itextpdf.io.image.ImageData;
import com.itextpdf.io.image.ImageDataFactory;
import com.itextpdf.kernel.colors.ColorConstants;
import com.itextpdf.kernel.events.Event;
import com.itextpdf.kernel.events.IEventHandler;
import com.itextpdf.kernel.events.PdfDocumentEvent;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfPage;
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
import com.itextpdf.layout.Canvas;
import com.itextpdf.layout.element.Image;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.property.TextAlignment;
import com.itextpdf.layout.property.VerticalAlignment;

import java.io.IOException;

/**
 * @description:
 */
public class WatermarkingEventHandler implements IEventHandler {

    private PdfFont font;

    private String watermark;
    private int watermarkFontSize = 32;

    private byte[] backgroundImageBytes;


    public WatermarkingEventHandler(PdfFont font, String watermark) {
        this.font = font;
        this.watermark = watermark;
    }


    public WatermarkingEventHandler(PdfFont font, String watermark, byte[] backgroundImageBytes) {
        this.font = font;
        this.watermark = watermark;
        this.backgroundImageBytes = backgroundImageBytes;
    }

    public WatermarkingEventHandler(PdfFont font, String watermark, int watermarkFontSize, byte[] backgroundImageBytes) {
        this.font = font;
        this.watermark = watermark;
        this.watermarkFontSize = watermarkFontSize;
        this.backgroundImageBytes = backgroundImageBytes;
    }

    @Override
    public void handleEvent(Event event) {

        PdfDocumentEvent docEvent = (PdfDocumentEvent) event;
        PdfDocument pdfDoc = docEvent.getDocument();
        PdfPage page = docEvent.getPage();
        try {
            if (this.font == null) {
                this.font = PdfFontFactory.createFont(StandardFonts.HELVETICA_BOLD);
            }
        } catch (IOException e) {
            // Such an exception isn't expected to occur,
            // because helvetica is one of standard fonts
            System.err.println(e.getMessage());
        }

        PdfCanvas canvas = new PdfCanvas(page.newContentStreamBefore(), page.getResources(), pdfDoc);

        float width = page.getPageSize().getWidth();
        float height = page.getPageSize().getHeight();

        float x = width / 2;
        float y = height / 2;

        Canvas canvas1 = new Canvas(canvas, pdfDoc, page.getPageSize());

        if (backgroundImageBytes != null && backgroundImageBytes.length > 0) {
            ImageData imageData = ImageDataFactory.create(backgroundImageBytes == null ? new byte[]{} : backgroundImageBytes);
            canvas1.add(new Image(imageData));
        }
        canvas1.setFontColor(ColorConstants.GREEN)
                .setFontSize(watermarkFontSize)
                .setFont(font)
                .showTextAligned(new Paragraph(watermark).setOpacity(0.5f), x, y, pdfDoc.getPageNumber(page),
                        TextAlignment.CENTER, VerticalAlignment.MIDDLE, 45)
                .close();
    }
}
